/**
 * 
 */
package wangjg.commons.dao;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.PrimaryKey;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Table;

public class HibernateConfigurationTools {

	private static Configuration hibernateConf;

	private static Configuration getHibernateConf() {
		if (hibernateConf == null) {
			Configuration cfg = new Configuration();
//			cfg.configure();
//			hibernateConf = cfg;
			return cfg;
		}
		return hibernateConf;
	}

	private static PersistentClass getPersistentClass(Class<?> clazz) {
		PersistentClass pc = getHibernateConf().getClassMapping(clazz.getName());
		if (pc == null) {
			synchronized (HibernateConfigurationTools.class) {
				getHibernateConf().addAnnotatedClass(clazz);
				pc = getHibernateConf().getClassMapping(clazz.getName());
			}
		}
		return pc;
	}

	/**
	 * 功能描述：获取实体对应的表名
	 * 
	 * @param clazz
	 *            实体类
	 * @return 表名
	 */
	@SuppressWarnings("unchecked")
	public static String getTableName(Class clazz) {
		return getPersistentClass(clazz).getTable().getName();
	}

	/**
	 * 功能描述：获取实体对应表的主键字段名称
	 * 
	 * @param clazz
	 *            实体类
	 * @return 主键字段名称
	 */
	@SuppressWarnings("unchecked")
	public static String getPkColumnName(Class clazz) {
		PersistentClass pc = getPersistentClass(clazz);
		Table table = pc.getTable();
		PrimaryKey pk = table.getPrimaryKey();
		Column col1 = pk.getColumn(0);
		return col1.getName();

	}

	/**
	 * 功能描述：通过实体类和属性，获取实体类属性对应的表字段名称
	 * 
	 * @param clazz
	 *            实体类
	 * @param propertyName
	 *            属性名称
	 * @return 字段名称
	 */
	@SuppressWarnings("unchecked")
	public static String getColumnName(Class clazz, String propertyName) {
		if(!StringUtils.isEmpty(propertyName)){
			
		}
		PersistentClass persistentClass = getPersistentClass(clazz);
		Property property = persistentClass.getProperty(propertyName);
		Iterator it = property.getColumnIterator();
		if (it.hasNext()) {
			Column column = (Column) it.next();
			return column.getName();
		}
		return null;
	}
	
	public static String getCommentContent(Class clazz,String fieldName){
		PersistentClass persistentClass = getPersistentClass(clazz);
		Property property = persistentClass.getProperty(fieldName);
		Iterator it = property.getColumnIterator();
		if (it.hasNext()) {
			Column column = (Column) it.next();
			return column.getComment();
		}
		return null;
	}
	/**
	 * 根据实体Class对象和数据库字段名得到实体属性名
	 * @param clazz
	 * @param attribName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getPropertyName(Class clazz, String attribName) {
		PersistentClass persistentClass = getPersistentClass(clazz);
		Iterator it = persistentClass.getPropertyIterator();
		//遍历Class对象的所有属性名
		while (it.hasNext()) {
			Property property = (Property) it.next();
			Iterator itr=property.getColumnIterator();
			//遍历该属性对应的数据库字段名
			while (itr.hasNext()) {
				Column c=(Column) itr.next();
				if(attribName.equals(c.getName())){
					return property.getName();
				}
			}
		}
		return null;
		
	}

}